<template>
  <div>
    <el-dialog
      :title="dialogAttr.title"
      :visible.sync="isOpen"
      width="60%"
      append-to-body
      :before-close="handleClose"
    >
      <div>
        <el-form
          :model="unBindQuQuery"
          ref="unBindQuQueryForm"
          size="medium"
          :inline="true"
          label-width="68px"
        >
          <el-form-item prop="searchValue">
            <el-input
              v-model="unBindQuQuery.searchValue"
              placeholder="请输入题干内容/知识点"
              clearable
              @keyup.enter.native="getQuList"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="medium" @click="getQuList">搜索</el-button>
            <el-button icon="el-icon-refresh" size="medium" @click="resetUnBindQuQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-row :gutter="16" class="drawer_el_row">
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-plus"
            size="mini"
            :disabled="multiple"
            @click="handleAddUnBindQu"
          >绑定</el-button>
        </el-col>
        <el-col :span="9">
          <div class="inner-select">
            <span>题目类型：</span>
            <el-select
              v-model="unBindQuQuery.quType"
              placeholder="请选择"
              size="small"
              @change="changeUnBindQuType"
            >
              <el-option
                v-for="dict in dict.type.qb_qu_type"
                :value="dict.value"
                :label="dict.label"
              ></el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="9">
          <div class="inner-select">
            <span>难度等级：</span>
            <el-select
              v-model="unBindQuQuery.quGrand"
              placeholder="请选择"
              size="small"
              @change="changeUnBindQuGrand"
              :clearable="true"
            >
              <el-option
                v-for="dict in dict.type.qu_qu_grand"
                :value="dict.value"
                :label="dict.label"
              ></el-option>
            </el-select>
          </div>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="quList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="题干内容" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <span>{{isObj(scope.row)?scope.row.objContent:scope.row.subContent}}</span>
          </template>
        </el-table-column>
        <el-table-column label="题目类型" align="center">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.qb_qu_type"
              :value="isObj(scope.row)?scope.row.objType:scope.row.subType"
            />
          </template>
        </el-table-column>
        <el-table-column label="难度等级" align="center" width="200">
          <template slot-scope="scope">
            <el-rate v-if="isObj(scope.row)" v-model="scope.row.objGrand" disabled />
            <el-rate v-else v-model="scope.row.subGrand" disabled />
          </template>
        </el-table-column>
        <el-table-column label="知识点" align="center" width="160px" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <my-tag :valStr="isObj(scope.row)?scope.row.objTag:scope.row.subTag" />
          </template>
        </el-table-column>
        <el-table-column v-if="quList.length != 0 &&  isObj(quList[0])" label="答案序号" align="center">
          <template slot-scope="scope">
            <el-tag>{{formatAnswer(scope.row)}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleQuInfo(scope.row)"
              v-hasPermi="['question:questionBank:query']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="handleQuAdd(scope.row)"
              v-hasPermi="['question:questionBank:add']"
            >绑定</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="unBindQuQuery.total>0"
        :total="unBindQuQuery.total"
        :page.sync="unBindQuQuery.pageNum"
        :limit.sync="unBindQuQuery.pageSize"
        @pagination="getQuList"
      />
    </el-dialog>
    <!-- 弹出详细信息弹窗 -->
    <obj-info :visible.sync="quInfo.objShow" :objInfo="quInfo.objData" />
    <sub-info :visible.sync="quInfo.subShow" :subInfo="quInfo.subData" />
  </div>
</template>

<script>
import ObjInfo from "@/components/QuInfo/objInfo";
import SubInfo from "@/components/QuInfo/subInfo";
export default {
  name: "UnBindQuList",
  dicts: ["qu_qu_grand", "qb_qu_type"],
  components: {
    ObjInfo,
    SubInfo
  },
  props: {
    unBindId: {
      type: Object,
      default: () => {
        return {
          type: "str",
          value: 0
        };
      }
    },
    // 已绑定题目列表
    getUnBindQuList: {
      type: Function,
      default: () => {}
    },
    // 解绑题目
    addUnBindQu: {
      type: Function,
      default: () => {}
    },
    // 弹框属性
    dialogAttr: {
      type: Object,
      default: () => {
        return {
          title: "",
          show: false
        };
      }
    }
  },
  data() {
    return {
      // 题目是否含有分数
      hasScore: {
        type: Boolean,
        default: false
      },
      // 绑定用户查询
      unBindQuQuery: {
        pageNum: 1,
        pageSize: 10,
        quType: 1,
        quGrand: undefined,
        searchValue: undefined,
        total: 0
      },
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 加载
      loading: false,
      // 用户列表
      quList: [],
      // 是否含有中间表id
      hasId: false,
      // 题目详情
      quInfo: {
        objShow: false,
        objData: {},
        subShow: false,
        subData: {}
      },
      unBindQuAddList: []
    };
  },
  methods: {
    /** 查询题目列表 */
    getQuList() {
      this.loading = true;
      this.getUnBindQuList(this.unBindQuQuery)
        .then(res => {
          this.quList = res.rows;
          this.unBindQuQuery.total = res.total;
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 重置未绑定题库搜索 */
    resetUnBindQuQuery() {
      this.resetForm("unBindQuQueryForm");
      this.unBindQuQuery.pageNum = 1;
      this.unBindQuQuery.quType = 1;
      this.unBindQuQuery.quGrand = undefined;
      this.getQuList();
    },
    /** 更改未绑定题目类型 */
    changeUnBindQuType() {
      this.getQuList();
    },
    /** 更改未绑定题目难度等级 */
    changeUnBindQuGrand() {
      this.getQuList();
    },
    /** 绑定 */
    handleQuAdd(row) {
      let isObj = this.isObj(row);
      let data = {
        quId: isObj ? row.objId : row.subId,
        quType: isObj ? row.objType : row.subType
      };
      data[this.unBindId.type] = this.unBindId.value;
      // 是否需要分数
      if (this.hasScore) {
        data["quScore"] = 10;
      }
      this.addUnBindQu([data])
        .then(res => {
          this.getQuList();
        })
        .finally(() => {});
    },
    /** 未绑定题目多选*/
    handleSelectionChange(data) {
      let datas = data.map(item => {
        let isObj = this.isObj(item);
        let formatData = {
          quId: isObj ? item.objId : item.subId,
          quType: isObj ? item.objType : item.subType
        };
        formatData[this.unBindId.type] = this.unBindId.value;
        // 是否需要分数
        if (this.hasScore) {
          formatData["quScore"] = 10;
        }
        return formatData;
      });
      this.single = data.length !== 1;
      this.multiple = !data.length;
      this.unBindQuAddList = datas;
    },
    handleAddUnBindQu() {
      let datas = this.unBindQuAddList;
      if (datas.length > 0) {
        this.$modal
          .confirm("是否绑定这" + datas.length + "道题目到题库中？")
          .then(() => {
            this.loading = true;
            this.addUnBindQu(datas)
              .then(() => {
                this.$modal.msgSuccess("绑定成功");
                this.getQuList();
              })
              .finally(() => {
                this.loading = false;
              });
          })
          .finally(() => {
            this.loading = false;
          });
      }
    },
    /** 预览题目 */
    handleQuInfo(row) {
      if (this.isObj(row)) {
        this.quInfo.objData = row;
        this.quInfo.objShow = true;
      } else {
        this.quInfo.subData = row;
        this.quInfo.subShow = true;
      }
    },
    /** 关闭时的回调 */
    handleClose() {
      this.unBindId.value = 0;
      this.dialogAttr.show = false;
    },
    /** 判断是主观题还是客观题 */
    isObj(data) {
      if (data.objType != undefined || data.objType != null) {
        return true;
      } else {
        return false;
      }
    },
    /** 答案序号格式化 */
    /** 更改答案显示序号 */
    formatAnswer(obj) {
      if (obj.objType == 1 || obj.objType == 3) {
        // 数字版本答案
        let ansN = obj.objAnswer.split("$");
        // 字母版答案
        let ansW = "";
        for (let i = 0; i < ansN.length; i++) {
          switch (ansN[i]) {
            case "1":
              ansW += "A";
              break;
            case "2":
              ansW += "B";
              break;
            case "3":
              ansW += "C";
              break;
            case "4":
              ansW += "D";
              break;
          }
        }
        return ansW;
      } else {
        return obj.objAnswer == "1" ? "√" : "×";
      }
    }
  },
  computed: {
    /** 弹框是否打开 */
    isOpen: {
      get() {
        return this.dialogAttr.show;
      },
      set(val) {
        this.$emit("update:visible", val);
      }
    }
  },
  watch: {
    unBindId: {
      handler(val) {
        if (val.value != 0 && val.value != "0") {
          let idType = val.type;
          this.unBindQuQuery[idType] = val.value;
          this.getQuList();
        }
      },
      deep: true,
      immediate: true
    }
  }
};
</script>

<style scoped>
.drawer_el_row {
  margin: 0 !important;
  margin-bottom: 10px !important;
}
</style>